<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>部门管理</title>
    <#include "../common/link.ftl"/>

    <script>
        $(function () {
            //去重
            //获取所有的权限
            var allPermissions =  $(".allPermissions option");
            //获取角色拥有的权限
            var selfPermissions = $(".selfPermissions option");
            //使用map函数将拥有权限返回一个新数组
            var selfPermissionIds = $.map(selfPermissions,function (Ele) {
                return Ele.value;
            })
            //遍历suo有的权限与拥有权限比对
            $.each(allPermissions,function (index,AllEle) {
                if($.inArray(AllEle.value,selfPermissionIds)>=0){
                    AllEle.remove();
                }
            })

            //提交表单
            $("#btn-submit").click(function () {
                //提交表单之前将角色拥有的权限设置为选中状态
                $(".selfPermissions option").prop("selected",true);
                $("#editForm").submit();
            })
        })

        //移动
        //全部移动
        function moveAll(src,target) {
            $("."+src+" option").appendTo($("."+target))
        }

        //选中移动
        function moveSelected(src,target) {
            $("." + src + " option:selected").appendTo($("." + target))
        }

    </script>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
   <#include "../common/navbar.ftl" />
    <!--菜单回显-->
    <c:set var="currentMenu" value="permission"/>
   <#include "../common/menu.ftl" />
    <div class="content-wrapper">
        <section class="content-header">
            <h1>角色编辑</h1>
        </section>
        <section class="content">
            <div class="box" style="padding: 10px;">
                <form class="form-horizontal" action="/role/saveOrUpdate" method="post" id="editForm">
                    <input type="hidden" name="id" value="${(role.id)!}">
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="name" class="col-sm-2 control-label">角色名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="name" name="name" value="${(role.name)!}"
                                   placeholder="请输入角色名称">
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="sn" class="col-sm-2 control-label">角色编码：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="sn" name="sn" value="${(role.sn)!}"
                                   placeholder="请输入角色编码">
                        </div>
                    </div>
                   
                    <div class="form-group " id="permission">
                        <label for="permission" class="col-sm-2 control-label">分配权限：</label><br/>
                        <div class="row" style="margin-top: 10px">
                            <div class="col-sm-2 col-sm-offset-2">
                                <select multiple class="form-control allPermissions" size="15">
                                    <%--所有权限--%>
                                    <#list roles as ro>
                                        <option value="${ro.id}">${ro.name}</option>
                                    </#list>
                                </select>
                            </div>

                            <div class="col-sm-1" style="margin-top: 60px;" align="center">
                                <div>

                                    <a type="button" class="btn btn-primary  " style="margin-top: 10px" title="右移动"
                                       onclick="moveSelected('allPermissions', 'selfPermissions')">
                                        <span class="glyphicon glyphicon-menu-right"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="左移动"
                                       onclick="moveSelected('selfPermissions', 'allPermissions')">
                                        <span class="glyphicon glyphicon-menu-left"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全右移动"
                                       onclick="moveAll('allPermissions', 'selfPermissions')">
                                        <span class="glyphicon glyphicon-forward"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全左移动"
                                       onclick="moveAll('selfPermissions', 'allPermissions')">
                                        <span class="glyphicon glyphicon-backward"></span>
                                    </a>
                                </div>
                            </div>

                            <div class="col-sm-2">
                                <select multiple class="form-control selfPermissions" size="15" name="Ids">

                                <#list (role.ps)! as roo>
                                        <option value="${roo.id}">${roo.name}</option>
                                   </#list>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-5">
                            <button id="btn-submit" type="submit" class="btn btn-primary">保存</button>
                            <button type="reset" class="btn btn-danger">重置</button>
                        </div>
                    </div>

                </form>
            </div>
        </section>
    </div>
   <#include "../common/footer.ftl" />
</div>
</body>
</html>